#!/bin/tcsh -ef
####
#
#############################################################################
#                                                                           #
# Title: Align w/ Unblur                                                    #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 07/21/2016                                             #
# Last Modification: 07/21/2016                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 30 
#
# MANUAL: This script runs a drift correction program on a movie file, to produce a drift-corrected movie and to produce a drift-corrected and averaged image file.
#
#
# PUBLICATION:<A HREF="http://grigoriefflab.janelia.org/unblur">Unblur Website</A>
#
# DISPLAY: imagenumber
# DISPLAY: imagename_original
# DISPLAY: comment
# DISPLAY: sample_pixel
# DISPLAY: movie_imagenumber_total
# DISPLAY: raw_gaincorrectedstack
# DISPLAY: SERIALEM_FACTOR
# DISPLAY: total_dose
# DISPLAY: frame_dose
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
set app_2dx_mrc_converter = ""
#
set tempkeep = ""
set imagename = ""
set nonmaskimagename = ""
set imagenumber = ""
set imagesidelength = ""
set sample_pixel = ""
set movie_stackname = ""
set movie_imagenumber_total = ""
set import_rawstack = ""
set raw_gaincorrectedstack = ""
set import_rawstack_type = ""
set SERIALEM_FACTOR = ""
set frame_dose = ""
set total_dose = ""
set import_original_time = ""
#
#$end_vars
#
set scriptname = import_driftcor_unblur
\rm -f LOGS/${scriptname}.results
#
source ${proc_2dx}/initialize
#
source ${proc_2dx}/2dx_makedirs
#
echo "<<@evaluate>>"
#
echo "<<@progress: 1>>"
#
if ( ! -e ${app_unblur} ) then
  ${proc_2dx}/protest "ERROR: First define location of UNBLUR in Settings"
endif
#
if ( ${movie_stackname} == "ScriptWillPutNameHere" ) then
  set movie_stackname = movie_aligned
  echo "set movie_stackname = ${movie_stackname}" >> LOGS/${scriptname}.results
endif
#
echo "# IMAGE-IMPORTANT: SCRATCH/${raw_gaincorrectedstack}.mrcs <Gain-corrected stack (stack)>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: dummy.mrc <Gain-corrected stack (stack)>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: ${raw_gaincorrectedstack}.mrc <Gain-corrected average image (2D)>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: ${raw_gaincorrectedstack}_fft.mrc <Gain-corrected average image FFT (2D)>" >> LOGS/${scriptname}.results
#
##########################################################################
${proc_2dx}/linblock "Calling unblur..."
##########################################################################
#
echo "<<@progress: 10>>"
#
\rm -f outdummy.mrc
\rm -f dummy.mrc
\rm -f unblur_shifts.txt
if ( ${import_rawstack_type} == "2" ) then
  ${dir_imod}/bin/clip unpack FASTDISK/${raw_gaincorrectedstack}.mrcs dummy.mrc 
else
  \ln -s FASTDISK/${raw_gaincorrectedstack}.mrcs dummy.mrc
endif
\rm -f ${movie_stackname}.mrc
#
echo ":: " ${app_unblur} 
echo ":: " SCRATCH/dummy.mrc
echo ":: " ${movie_imagenumber_total}
echo ":: " ${movie_stackname}.mrc
echo ":: " unblur_shifts.txt
echo ":: " ${sample_pixel}
echo ":: " YES
echo ":: " ${frame_dose}
echo ":: " 300
echo ":: " 0
echo ":: " YES
echo ":: " outdummy.mrc
echo ":: " NO
echo ":: " 
#
${app_unblur} << eof 
dummy.mrc
${movie_imagenumber_total}
${movie_stackname}.mrc
unblur_shifts.txt
${sample_pixel}
YES
${frame_dose}
300
0
YES
outdummy.mrc
NO
eof
#
\rm -f dummy.mrc
\mv -f outdummy.mrc ${movie_stackname}.mrcs
#
echo "# IMAGE-IMPORTANT: FASTDISK/${raw_gaincorrectedstack}.mrcs <Raw movie (stack)>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: ${movie_stackname}.mrcs <Drift-corrected movie (stack)>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: unblur_shifts.txt <Unblur determined shifts (TXT)>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: ${movie_stackname}.mrc <Drift-corrected average image (2D)>" >> LOGS/${scriptname}.results
#
##########################################################################
${proc_2dx}/linblock "2dx_mrc_converter - to create a PNG file"
##########################################################################
#
echo "<<@progress: 50>>"
#
${app_2dx_mrc_converter} --size 400 ${movie_stackname}.mrc ${movie_stackname}.mrc.png 
#
##########################################################################
${proc_2dx}/linblock "${proc_2dx}/unblur_plotter.py ${imagename}_unblur_shifts.txt translations.png"
##########################################################################
#
echo "<<@progress: 70>>"
#
\rm -f translations.png
${app_python}  ${proc_2dx}/unblur_plotter.py unblur_shifts.txt translations.png
#
echo "# IMAGE-IMPORTANT: translations.png <drift plot (PNG)>" >> LOGS/${scriptname}.results
#
echo "<<@progress: 50>>"
echo "<<@evaluate>>"
#
##########################################################################
${proc_2dx}/linblock "clip fft ${movie_stackname}.mrc ${movie_stackname}_fft.mrc"
##########################################################################
#
echo "<<@progress: 80>>"
#
\rm -f ${movie_stackname}_fft.mrc
${dir_imod}/bin/clip fft ${movie_stackname}.mrc ${movie_stackname}_fft.mrc
echo "# IMAGE-IMPORTANT: ${movie_stackname}_fft.mrc <Drift-corrected average image FFT (2D)>" >> LOGS/${scriptname}.results
#
##########################################################################
${proc_2dx}/linblock "Prepare status images."
##########################################################################
#
${app_2dx_mrc_converter} --size 400 ${movie_stackname}.mrc STATUS/2-image.jpg
${app_2dx_mrc_converter} --size 400 ${movie_stackname}_fft.mrc STATUS/2-FFT.jpg
\cp -f translations.png STATUS/3-image.png
#
set movie_enable = "y"
echo "set movie_enable = ${movie_enable}" >> LOGS/${scriptname}.results
#
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
#
echo "<<@progress: 100>>"
echo "<<@evaluate>>"
exit
#
# These are listed here to make sure they appear in the 2dx_image GUI:
#
